// CommodityServiceImpl.java (服务实现类)
package com.ywb.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ywb.mapper.CommodityMapper;
import com.ywb.pojo.Category;
import com.ywb.pojo.Commodity;
import com.ywb.pojo.CommodityQueryParam;
import com.ywb.pojo.PageResult;
import com.ywb.service.CommodityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CommodityServiceImpl implements CommodityService {

    @Autowired
    private CommodityMapper commodityMapper;

    @Override
    public List<Commodity> findAll() {
        return commodityMapper.findAll();
    }

    @Override
    public void deleteById(Integer id) {
        commodityMapper.deleteById(id);
    }

    @Override
    public void add(Commodity commodity) {
        commodityMapper.insert(commodity);
    }

    @Override
    public Commodity getById(Integer id) {
        return commodityMapper.getById(id);
    }

    @Override
    public void update(Commodity commodity) {
        commodityMapper.update(commodity);
    }

    @Override
    public PageResult<Commodity> page(CommodityQueryParam commodityQueryParam) {
        PageHelper.startPage(commodityQueryParam.getPage(), commodityQueryParam.getPageSize());
        List<Commodity> categories = commodityMapper.list(commodityQueryParam);
        Page<Commodity> p= (Page<Commodity>) categories;
        return new PageResult<>(p.getTotal(),p.getResult());
    }
}